package com.coldworks.coldjoke.ui;

import com.coldworks.base.manager.BaseBitmapManager;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.manager.ShareManager;
import com.coldworks.coldjoke.model.JokeModel;
import com.coldworks.coldjoke.util.CONST;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.umeng.socialize.bean.ShareImage;
import com.umeng.socialize.bean.ULocation;
import com.umeng.socialize.controller.SocializeController;
import com.umeng.socialize.controller.OauthHelper.SHARE_MEDIA;
import com.umeng.socialize.controller.SocializeController.RequestType;

import android.content.ClipData;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MenuShareJokeView {
	private Context context;
	private PopupWindow mPopup;
	private SocializeController controller = SocializeController.getController(
			"Android", RequestType.SOCIAL);
	private IWXAPI api;
	private JokeModel jokeModel;
	private String shareContent;
	private ShareImage shareImage;
	private ULocation uLocation;

	public MenuShareJokeView(Context context) {
		this.context = context;
		api = WXAPIFactory.createWXAPI(context, CONST.APP_ID.WEIXIN_APP_ID);
		initMenu();
	}

	/**
	 * 是否正在显示菜单
	 */
	public boolean isShowing() {
		return false;
	}

	/**
	 * 让菜单消失
	 */
	public void dismiss() {
		if (mPopup != null && !mPopup.isShowing()) {
			mPopup.dismiss();
		}
	}

	public void show(View parent, JokeModel jokeInfo, ULocation uLocation) {
		this.uLocation = uLocation;
		this.jokeModel = jokeInfo;
		if (!mPopup.isShowing()) {
			mPopup.showAtLocation(parent, Gravity.BOTTOM, 0, 80);
		}
	}

	private void initMenu() {
		LinearLayout vMenu;
		vMenu = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.menu_share, null);

		vMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPopup.dismiss();
			}
		});

		vMenu.findViewById(R.id.menu_share_item_copy).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
							android.content.ClipboardManager clip = (android.content.ClipboardManager) context
									.getSystemService(Context.CLIPBOARD_SERVICE);
							ClipData clipData = ClipData.newPlainText("joke",
									jokeModel.getJokeText());
							clip.setPrimaryClip(clipData);
							Toast.makeText(context, "已成功复制到剪切板",
									Toast.LENGTH_SHORT).show();
						} else {
							android.text.ClipboardManager clip = (android.text.ClipboardManager) context
									.getSystemService(Context.CLIPBOARD_SERVICE);
							clip.setText(jokeModel.getJokeText()); // 复制
							Toast.makeText(context, "已成功复制到剪切板",
									Toast.LENGTH_SHORT).show();
						}

					}
				});

		vMenu.findViewById(R.id.menu_share_item_mms).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						ShareManager.getInstance().shareToMMS(context,
								jokeModel.getJokeText(), getShareUrl(),
								jokeModel.hasImage());
					}
				});
		vMenu.findViewById(R.id.menu_share_item_qzone).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.QZONE)) {
							ShareManager.getInstance().shareToQzone(context,
									controller, getShareJokeContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.QZONE,
									getShareJokeContent(), getShareImage(),
									null);
						}
					}
				});
		vMenu.findViewById(R.id.menu_share_item_renren).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.RENREN)) {
							ShareManager.getInstance().shareToRenren(context,
									controller, getShareJokeContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.RENREN,
									getShareJokeContent(), getShareImage(),
									null);
						}
					}
				});
		vMenu.findViewById(R.id.menu_share_item_sina).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.SINA)) {
							ShareManager.getInstance().shareToWeibo(context,
									controller, getShareJokeContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.SINA,
									getShareJokeContent(), getShareImage(),
									null);
						}

					}
				});
		vMenu.findViewById(R.id.menu_share_item_social).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						ShareManager.getInstance().shareToWeiXinFriend(context,
								api, getShareToWXJokeUrl(),
								jokeModel.getJokeText(),
								jokeModel.getJokeText(), getShareThumbnail());
					}
				});
		vMenu.findViewById(R.id.menu_share_item_t).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						if (ShareManager.getInstance().isOauthed(context,
								SHARE_MEDIA.TENCENT)) {
							ShareManager.getInstance().shareToTWeibo(context,
									controller, getShareJokeContent(),
									getShareImage(), null);
						} else {
							ShareManager.getInstance().doOauthVerify(context,
									controller, SHARE_MEDIA.TENCENT,
									getShareJokeContent(), getShareImage(),
									null);
						}
					}
				});
		vMenu.findViewById(R.id.menu_share_item_weixin).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						mPopup.dismiss();
						ShareManager.getInstance().shareToWeixinPerson(context,
								api, getShareToWXJokeUrl(), null,
								jokeModel.getJokeText(), getShareThumbnail());
					}
				});

		mPopup = new PopupWindow(vMenu, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		mPopup.update();
		mPopup.setFocusable(true);
		mPopup.setAnimationStyle(android.R.style.Animation_Toast);
		mPopup.setBackgroundDrawable(new BitmapDrawable());
	}

	private ShareImage getShareImage() {
		if (jokeModel.hasImage()) {
			byte[] bytes = BaseBitmapManager.getInstance().getBytesByFileName(
					jokeModel.getImgPath(), 0, -1);
			shareImage = new ShareImage(context, bytes);
			return shareImage;
		} else {
			return null;
		}

	}

	// TODO 字数太多了怎么办
	private String getShareJokeContent() {
		if (jokeModel.getJokeText() == null) {
			shareContent = "内容来自于  " + getShareUrl();
		} else {
			shareContent = jokeModel.getJokeText() + " 内容来自于  " + getShareUrl();
		}
		return shareContent;
	}

	private String getShareUrl() {
		return "http://lengxiaohua.com/p/joke/" + jokeModel.getJokeId();
	}

	private String getShareToWXJokeUrl() {
		return "http://m.lengxiaohua.com/p/joke/" + jokeModel.getJokeId();
	}

	private Bitmap getShareThumbnail() {
		if (!jokeModel.hasImage()) {
			return null;
		}
		Bitmap bitmap = BaseBitmapManager.getInstance().getThumbnailByPath(
				context, jokeModel.getImgPath(), 100, 100);
		return bitmap;
	}

}